package com.jinxiangyang.controller;

import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.vo.CacheVo;
import com.jinxiangyang.service.CacheService;
import com.jinxiangyang.service.impl.CacheServiceImpl;
import com.jinxiangyang.utils.StringUtils;
import com.jinxiangyang.utils.constants.CacheConstants;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author jinxiangyang
 * @date 2023/8/17 14:22
 * @description TODO
 **/


@RestController
@RequestMapping("/cache")
@Api(tags = {"缓存监控"})
public class CacheController {



    @Autowired
   private RedisTemplate<String, String> redisTemplate;



    @Autowired
    private CacheService cacheService;



    @GetMapping("/info")
    @ApiOperation("redis配置信息")
    @OperateLog(title = "缓存模块",businessType = BusinessType.OTHER,operatorType = OperatorType.ADMIN)
    public AjaxResult getInfo() {
        Properties info = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info());
        Properties commandStats = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info("commandstats"));
        Object dbSize = redisTemplate.execute((RedisCallback<Object>) connection -> connection.dbSize());
        Map<String, Object> result = new HashMap<>(3);
        result.put("info", info);
        result.put("dbSize", dbSize);
        List<Map<String, String>> pieList = new ArrayList<>();
        commandStats.stringPropertyNames().forEach(key -> {
            Map<String, String> data = new HashMap<>(2);
            String property = commandStats.getProperty(key);
            data.put("name", StringUtils.removeStart(key, "cmdstat_"));
            data.put("value", StringUtils.substringBetween(property, "calls=", ",usec"));
            pieList.add(data);
        });
        result.put("commandStats", pieList);
        return AjaxResult.success(result);
    }




    @PostMapping( "/create")
    @ApiOperation("添加缓存")
    @OperateLog(title = "缓存模块",businessType = BusinessType.INSERT,operatorType = OperatorType.ADMIN)
    public AjaxResult cacheValueCreate(@RequestBody CacheVo vo) {
        return cacheService.cacheValueCreate(vo);
    }


    @GetMapping("/get/{cacheName}")
    @ApiOperation("获取缓存")
    @OperateLog(title = "缓存模块",businessType = BusinessType.OTHER,operatorType = OperatorType.ADMIN)
    public AjaxResult cacheValueGet(@PathVariable String cacheName){
        Set<String> cacheKeys = redisTemplate.keys(cacheName + "*");
        return AjaxResult.success(cacheKeys);
    }



    @DeleteMapping("/delAll")
    @ApiOperation("清空缓存")
    public AjaxResult cacheDeleteAll(){
        Collection<String> cacheKeys = redisTemplate.keys("*");
        redisTemplate.delete(cacheKeys);
        return AjaxResult.success();
    }


    @DeleteMapping("/del")
    @ApiOperation("删除指定的key")
    public AjaxResult cacheValueDel(@RequestBody CacheVo cacheVo){
        return cacheService.cacheValueDelete(cacheVo);
    }




}
